package com.kinptm.masterclock.bean;

import android.content.Context;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

/**
 * Created by pc on 13-11-18.
 */
@DatabaseTable
public class TimerRecord extends AlarmRang {
    @DatabaseField
    private int day;
    @DatabaseField
    private int hour;
    @DatabaseField
    private int minute;
    @DatabaseField
    private int second;
    @DatabaseField(dataType = DataType.SERIALIZABLE)
    private DateTime time;
    @DatabaseField(dataType = DataType.SERIALIZABLE)
    private Period period;
    @DatabaseField
    private boolean finish;

    public TimerRecord(){}

    public TimerRecord(Context context) {
        this(context, 0, 0, 0, 0);
    }

    public TimerRecord(Context context, int minute, int second) {
        this(context, 0, 0, minute, second);
    }

    public TimerRecord(Context context, int hour, int minute, int second) {
        this(context, 0, hour, minute, second);
    }

    public TimerRecord(Context context, int day, int hour, int minute, int second) {
        super(context);
        update(day, hour, minute, second);
    }

    public void updateItSelf() {
        update(day, hour, minute, second);
    }

    public void update(int day, int hour, int minute, int second) {
        DateTime now = DateTime.now();
        time = now.plusDays(day).plusHours(hour).plusMinutes(minute).plusSeconds(second);

        period = new Period(now, time);

        setDay(period.getDays());
        setHour(period.getHours());
        setMinute(period.getMinutes());
        setSecond(period.getSeconds());
    }


    public Period getPeriod() {
        if (period.toStandardSeconds().getSeconds() == 0) {
            updateItSelf();
        }
        return period;
    }

    public PeriodFormatter getPeriodFormatter() {
        return buildPeriodFormatter(period);
    }

    public DateTime getTime() {
        return time;
    }

    private PeriodFormatter buildPeriodFormatter(Period period) {
        PeriodFormatterBuilder periodFormatterBuilder = new PeriodFormatterBuilder().printZeroAlways().minimumPrintedDigits(2);


        if (period.toStandardDays().getDays() > 0) {
            periodFormatterBuilder.appendDays().appendLiteral("天");
        }

        if (period.toStandardHours().getHours() > 0) {
            periodFormatterBuilder.appendHours()
                    .appendLiteral("小时 ");

        }

        periodFormatterBuilder.appendMinutes()
                .appendLiteral("分钟 ").appendSeconds().appendLiteral("秒 ");
        return periodFormatterBuilder.toFormatter();
    }

    @Override
    public String toString() {
        return "TimerRecord{" +
                "day=" + day +
                ", hour=" + hour +
                ", minute=" + minute +
                ", second=" + second +
                ", time=" + time +
                ", period=" + period +
                ", finish=" + finish +
                "} " + super.toString();
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
        if (enable) {
            updateItSelf();
        } else {
        }
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }

    public boolean isFinish() {
        return finish;
    }

    public void setFinish(boolean finish) {
        this.finish = finish;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof TimerRecord)) return false;

        TimerRecord that = (TimerRecord) o;

        if (id != that.id) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id;
    }
}
